package com.qf.zyh.services.Impl;

import com.qf.zyh.common.Pages;
import com.qf.zyh.dao.Impl.ProductDaoImpl;
import com.qf.zyh.dao.ProductDao;
import com.qf.zyh.entity.Product;
import com.qf.zyh.services.ProductSer;
import com.qf.zyh.util.DBUtil;
import com.qf.zyh.util.StrUtils;
import org.apache.commons.dbutils.DbUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductSerImpl implements ProductSer {
    ProductDao pd = new ProductDaoImpl();
    @Override
    public Product getid(Integer pid) {
        try {
            DBUtil.begin();
            Product pdd = pd.getid(pid);
            DBUtil.commit();
            return pdd;
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
return null;
    }

    @Override
    public Map<String, Object> getProPage(Integer tid, Integer pageIndex, int i) {
        //先获取数据总行数，然后创建页数对象。然后根据条件搜索每页的数据
        //然后创建map集合，存储页面数据和页数数据
        Map<String,Object> maps = null;
        try {
            DBUtil.begin();
            int count = pd.countid(tid).intValue();
            Pages pg=new Pages(pageIndex,i,count);
            List<Product> list = pd.selectpage(tid,pg.getStartRow(),pg.getPageSize());
            maps = new HashMap<>();
            maps.put("list",list);
            maps.put("page",pg);
            DBUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return maps;
    }
}
